package controller;

import java.awt.image.BufferedImage;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;

import model.PicDatabaseModel;
import utils.Picture;
import cameraConfig.Kamera;
import cameraConfig.KameraDAO;

public class PictureControl {
	private static PictureControl instance;
	
	private PicDatabaseModel dmPicture;
	private HashMap<Integer, Vector<Picture>> hmPicture = new HashMap<Integer, Vector<Picture>>();;
	
	
	public static PictureControl getInstance() {
		if(instance == null) {
			instance = new PictureControl();
		}
		
		return instance;
	}
	
	private PictureControl() {
		dmPicture = new PicDatabaseModel();
	}
	
	public boolean InsertNewPicture( String strPicLocation, 
			String strPicPosition,
			java.util.Date picDate,
			BufferedImage image ) {
		
		if( strPicLocation == null )
			return false;
		if( strPicPosition == null )
			return false;
		if( picDate == null )
			return false;
		if( image == null )
			return false;
		
		dmPicture.InsertPicture(strPicLocation, strPicPosition, picDate, image) ;
		
		return true;
	}
	
	public void DeletePicture() {
		
	}
	
	public HashMap<Integer, Vector<Picture>>  SearchDatabaseForPictures( String strPicLocation,
			int[] camIDs,
			Date dateFrom,
			Date dateTo ) {
		
		hmPicture.clear();
		
		if(camIDs == null) {
			KameraDAO camDao = new KameraDAO();
			
			Vector<Kamera> temp = camDao.getCamObjectsIn(camDao.getLocationByName(strPicLocation));
			
			camIDs =  new int[temp.size()];
			int i = 0;
			
			for (Iterator<Kamera> iterator = temp.iterator(); iterator.hasNext();i++) {
				Kamera kamera = (Kamera) iterator.next();
				
				camIDs[i] = kamera.getCamID();
			}
		}
		
		
		for( int i = 0; i < camIDs.length; i++ ) {
			hmPicture.put(camIDs[i], new Vector<Picture>(dmPicture.GetPictureFromTo(strPicLocation, Integer.toString(camIDs[i]), dateFrom, dateTo)));
		}
		
		
//		Vector<Picture> picList = new Vector<Picture>(dmPicture.GetPictureFromTo(strPicLocation, strPicPosition, dateFrom, dateTo));
		
		return hmPicture;
	}

}
